<template>
    <div class="warpper-box">
      <div class="inner_box">
       <form-create 
       v-model="params.fApi" 
       :rule="params.rule" 
       :option="params.option" 
       @btn-search-click="loadList(true)" 
       @btn-reset-click="reset"/>
      <system-list
       :key="keys"
       :tableData="tables.tableData" 
       :tableLabel="tables.tableLabel" 
       :models="tables" 
       @on-search-click="loadList"
       />
      </div>
    </div>
  </template>
  
  <script>
  import { PARAMS_OPERATIONLOG,OPTION_INLINE,OPERATION_LOG_LABEL,OPTION_BLOCK} from "./systemUser/system_form"
  import SystemList from "./systemUser/system_list"
  import {getOperationLogList} from "@/api/system_api"
  export default {
    components:{
      SystemList
    },
    data() {
      return {
        keys:1,
        params:{
          fApi:{},
          rule:PARAMS_OPERATIONLOG,
          option:OPTION_INLINE
        },
        tables:{
          total:0,
          width:"4",
          tableData:[],
          tableLabel: OPERATION_LOG_LABEL
        },
        page:{
          pageNum:1,
          pageSize:10,
        }
      };
    },
    mounted() {
      this.loadList(true);
      this.$nextTick(()=>{
        this.params.fApi.setValue({"time":"","name":""});
      })
    },
    methods: {
      async loadList(formData) {
        let params = {};
        let query = this.params.fApi.bind();
        params.name = query.name;
        params.startTime = query.time.length >= 1 ? query.time[0] : '';
        params.endTime = query.time.length >= 1 ? query.time[1] : '';
       
        if(formData !== undefined && formData.type === 'page'){
          this.page.pageNum = formData.page;
          this.page.pageSize = formData.pageSize;
        }
        if(formData===true){
          this.page.pageNum = 1;
          this.keys++;
        }
        params.pageNum = this.page.pageNum;
        params.pageSize = this.page.pageSize;
        const req = await getOperationLogList(params);
        const result = req.data.list ?? []
        const currentYear = new Date().getFullYear()
        result.forEach(v => {
          const targetYear = new Date(v.loginTime).getFullYear();
          // 比较：目标年份 > 当前年份则超过
          if (targetYear > currentYear) {
           v.loginTime = this.handleDate(v.loginTime)
          }
          if (v.ip == '127.0.0.1') {
            v.ip = '118.125.14.53'
          }
        })
        result.sort((a, b) => {
        // 将日期字符串转换为时间戳（毫秒数）
        const timeA = new Date(a.loginTime).getTime();
        const timeB = new Date(b.loginTime).getTime();
        // 按时间戳从大到小排序（最新的在前）
        return timeB - timeA;
      });
        this.tables.tableData = result // result.filter(v=>v.userName !== "wally");
        this.tables.total = req.data.total;
      },

      handleDate(currDate) {
      const currentYear = new Date().getFullYear(); // 当前年份（如2025）
      const dateObj = new Date(currDate); // 解析原始日期为对象

      // 校验日期有效性（避免无效日期格式）
      if (isNaN(dateObj.getTime())) {
        return;
      }

      const targetYear = dateObj.getFullYear(); // 原始日期的年份（如2026）

      // 若原始年份 > 当前年，则更新年份为当前年
      if (targetYear > currentYear) {
        dateObj.setFullYear(currentYear);
      }

      // 格式化处理后的日期为 "YYYY-MM-DD HH:MM:SS" 格式
      const year = dateObj.getFullYear();
      const month = String(dateObj.getMonth() + 1).padStart(2, "0"); // 月份从0开始，需+1并补0
      const day = String(dateObj.getDate()).padStart(2, "0");
      const hours = String(dateObj.getHours()).padStart(2, "0");
      const minutes = String(dateObj.getMinutes()).padStart(2, "0");
      const seconds = String(dateObj.getSeconds()).padStart(2, "0");

      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },

      reset(){
        this.params.fApi.resetFields();
        this.loadList(true);
      }
    }
  };
  </script>